﻿@model CCTV.Common.CameraXML
@using CCTV.Web;
@using CCTV.Common;
@using CCTV.Logic;
@{
    ViewBag.Title = "VideoControl";
}

@{ var cameraType = "_"+Model.Cameras[Model.SelectedCamera].CameraType.ToString() + "UserControl";
   var cams = new List<SelectListItem>();
   foreach (CCTV.Common.CCTVCamera cam in Model.Cameras)
   {
       cams.Add(new SelectListItem() { Text = cam.CameraName, Value = cam.CameraNumber.ToString() });
   }
}

<script type="text/javascript">
    $(function () {
        // Tabs
        $('#tabs').tabs();

        $("#sliderFPS").slider({
            range: "min",
            value: 2,
            min: 1,
            max: 15,
            slide: function (event, ui) {
                $("#lblFramerate").html(ui.value + " fps");
                frameInterval = 1000 / ui.value;
                clearInterval(refreshIntervalId);
                refreshIntervalId = setInterval(reloadImage, frameInterval);
            }
        });

        $("#lblFramerate").html("1 fps");
    });

    function computeFrameRate() {
        var start = new Date().getTime();
        $.ajax({
            type: "GET",
            cache: "false",
            url: "@Url.Action("GetImage", "Home")",
            complete: function (jqXHR, textStatus) {
                var duration = (new Date().getTime() - start);
                frameInterval = 1000 / duration;
                alert("took '" + duration + "' msecond(s) to load");
            }
        });
    }

    var frameInterval = 500; //2fps default...
	var selectedCamNo = '@Html.Raw(Model.Cameras[Model.SelectedCamera].CameraNumber.ToString())';
    var selectedSpeed = '@Html.Raw(Model.Cameras[Model.SelectedCamera].DefaultMovementSpeed.ToString())';
    var refreshIntervalId;


    function callServer(url2) {
	    $.ajax({
            type: "GET",
            cache: 'false',
            url: url2,
            complete: function (data, jqXHR, textStatus) {
            }
        });
	}

	function callServerButton(cmd) {
	    var params = {};
	    params.CameraNumber = selectedCamNo;
	    params.CameraCommand = cmd;
	    params.CameraSpeed = selectedSpeed;
	    $.ajax({
	        type: "GET",
	        cache: "false",
	        data: params,
	        url: "@Url.Action("PassCameraCommand","Home")?avoidcache=" + getTimestamp(),
	        complete: function (jqXHR, textStatus) {
	        }
	    });

	}

    
    function getPano(cmd, camPos) {
	    var params = {};
	    params.CameraNumber = selectedCamNo;
	    params.CameraCommand = cmd;
	    params.CameraSpeed = camPos;
	    $.ajax({
	        type: "GET",
	        cache: "false",
	        data: params,
	        url: "@Url.Action("PassCameraCommand","Home")?avoidcache=" + getTimestamp(),
	        complete: function (jqXHR, textStatus) {
	        }
	    });

	}


	//////////////////////////////////////////////VIDEO COMMANDS

	function reloadImage() {
	    d = new Date();
	    $("#picVideoImage").attr("src", "@Url.Action("GetImage","Home")?" + d.getTime());
	}

	function callServerVideoCommand(cmd) {
	    callServer('@Url.Action("VideoCommand","Home")/' + cmd);
	    if (cmd == '@Html.Raw(AppConstants.VideoCommandsEnum.Play.ToString())') {
	        //start the timer...
	        refreshIntervalId = setInterval(reloadImage, frameInterval);
	    }
	    else {
	        //stop the timer
	        clearInterval(refreshIntervalId);
	    }
	}

	function callServerVideoSize(size) {
	    callServer('@Url.Action("ChangeSize","Home")/' + size);

	}

	function callServerVideoCompression(compression) {
	   callServer('@Url.Action("ChangeCompression","Home")/' + compression);
    }
    ///////////////////////////////////END VIDEO COMMANDS

    function changeCamera(camNo) {
        $('#tab1').load('@Url.Action("GetUserControlForCam", "Home")?CameraNumber=' + camNo + '&avoidcache=' + getTimestamp());
        //reselect first tab
        $('#tabs').tabs('select', 0);
        //change the style of the preview
        var selId = "#camSel" + camNo;
        $(".camthumb").removeClass('ui-widget-header').addClass('ui-widget-header');
        $(selId).removeClass('ui-widget-header');
        selectedCamNo = camNo;

    }

    function getTimestamp() {
        tstmp = new Date();
        return tstmp.getTime();
    } 
</script>

@if (User.IsInRole(SiteRoles.Admin))
{
    <script type="text/javascript">
        function addUser() {
            var usrN = $("#txtUserToAdd").val();
            $.ajax({
                type: "GET",
                cache: "false",
                url: "/Home/AddUser/" + usrN+"?avoidcache=" + getTimestamp(),
                complete: function (jqXHR, textStatus) {
                   reloadUsers();
                   $("#txtUserToAdd").attr("value", ""); //clean the textbox
                }
            });
         }

            function deleteUser(name) {
                $.ajax({
                    type: "GET",
                    cache: "false",
                    url: "/Home/DeleteUser/" + name+"?avoidcache=" + getTimestamp(),
                    complete: function (jqXHR, textStatus) {
                        reloadUsers();
                    }
                });
            }


            function reloadUsers() {

                $('#divUsers').load('@Url.Action("ListUsers", "Home")?avoidcache=' + getTimestamp());
            }

            function sendSystemCommand(cmd) {
                callServer('/Home/PassMainCommand/' + cmd);
            }
    </script>
}
<table style="width: 1100px; background-color: white" align="center">
    <tr>
        <td style="width:280px" align="center">
            <label class="smalllabel">Video:</label><br />
             @Html.NormalButton("btnPlay", "Play",
                           Utilities.GetVideoStateFunction(AppConstants.VideoCommandsEnum.Play),
                                        "smallbuttonsmalltext")
        
             @*@Html.NormalButton("btnPause", "||",
                           Utilities.GetVideoStateFunction(AppConstants.VideoCommandsEnum.Pause),
                                        "smallbuttonsmalltext")
                                        *@
                                        
             @Html.NormalButton("btnStop", "Stop",
                           Utilities.GetVideoStateFunction(AppConstants.VideoCommandsEnum.Stop),
                                        "smallbuttonsmalltext")
            <br />
            <br />
            <label class="smalllabel">Framerate: </label><label class="smalllabel" id="lblFramerate"></label>
            <br />
            <br />
            <div id="sliderFPS"></div><br />
            <br />
            <br />
            <label class="smalllabel">Size:</label><br />
             @Html.NormalButton("btnSizeS", "S",
                          Utilities.GetVideoSizeJSFunction(AppConstants.VideoSizeEnum.Small),
                      "smallbutton")
             @Html.NormalButton("btnSizeM", "M",
                           Utilities.GetVideoSizeJSFunction(AppConstants.VideoSizeEnum.Medium),
                      "smallbutton")
             @Html.NormalButton("btnSizeL", "L",
                           Utilities.GetVideoSizeJSFunction(AppConstants.VideoSizeEnum.Large),
                               "smallbutton")
                               <br /><br />
                               <label class="smalllabel">Compression:</label><br />
             @Html.NormalButton("btnCompresionLow", "S",
                                    Utilities.GetVideoCompressionJSFunction(AppConstants.VideoCompressionEnum.Bad),
                      "smallbutton")
             @Html.NormalButton("btnCompressionMedium", "M",
                                   Utilities.GetVideoCompressionJSFunction(AppConstants.VideoCompressionEnum.Medium),
                      "smallbutton")
             @Html.NormalButton("btnCompressionNormal", "N",
                                    Utilities.GetVideoCompressionJSFunction(AppConstants.VideoCompressionEnum.Good),
                               "smallbutton")
             @Html.NormalButton("btnCompressionBest", "H",
                                    Utilities.GetVideoCompressionJSFunction(AppConstants.VideoCompressionEnum.Best),
                               "smallbutton")
                               
        </td>
        <td style="height:480px; width:640px;" align="center" id="containerPic">
            <img src="@Url.Content("~/Content/images/placeholder.jpg")" id="picVideoImage" alt="videoStream" />
        </td>
        <td align="right" valign="top">
        <div id="tabs" style="margin: 0px; padding: 0px;  border: 0px solid white !important">
        	<ul>
				<li><a href="#tab1">Control</a></li>
				<li><a href="#tab2">Select camera</a></li>
                @if (User.IsInRole(SiteRoles.Admin))
                {
                    <li><a href="#tab3">Admin</a></li>
                }
			</ul>
			    <div id="tab1" class="tabitem" style="text-align:center">
                @Html.Partial(cameraType, Model.Cameras[Model.SelectedCamera])
                </div>
			    <div id="tab2" class="tabitem">
                @for(int i=0; i<Model.Cameras.Count;i++)
                {
                    var camNo = Model.Cameras[i].CameraNumber.ToString();
                    <div id='@Html.Raw("camSel"+camNo)' class="camthumb @Html.Raw(i!=Model.SelectedCamera ? "ui-widget-header":"")" onclick="changeCamera('@Html.Raw(camNo)');">@Html.Raw(Model.Cameras[i].CameraName)</div>
                    
                }
                </div>
                <div id="tab3" class="tabitem">
                    <table style="width: 100%; padding:0px; margin:0px; border: 0px !important" align="center">
                        <tr>
                            <td align="center">
                                <input type="button" value="CAM ON" onclick="sendSystemCommand(@Html.Raw("'"+AppConstants.CustomPICBoardCommandsEnum.On.ToString()+"'"))"/>
                                <input type="button" value="CAM OFF" onclick="sendSystemCommand(@Html.Raw("'"+AppConstants.CustomPICBoardCommandsEnum.Off.ToString()+"'"))"/>
                            </td>
                        </tr>
                        <tr>
                            <td align="center">
                                <input type="button" value="TURN OFF PC" />
                            </td>
                        </tr>
                        <tr>
                            <td align="center">
                                <label class="smalllabel">System Users:</label>
                                <br />
                                <div id="divUsers">
                                @Html.Partial("_UserGrid", CommandManager.GetApplicationUsers())
                                </div>
                            </td>
                        </tr>
                    </table>
                    
                </div>
            </div>
		</td>
    </tr>
</table>